/*
 * Copyright 2025 The Netty Project
 *
 * The Netty Project licenses this file to you under the Apache License,
 * version 2.0 (the "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at:
 *
 *   https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.netty.handler.codec.http3;

import org.junit.jupiter.api.Test;

import java.util.Iterator;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertNotSame;

/**
 * unit tests for {@link DefaultHttp3SettingsFrame}.
 */
class DefaultHttp3SettingsFrameTest {

    @Test
    void testDefaultFrameHasEmptySettings() {
        DefaultHttp3SettingsFrame frame = new DefaultHttp3SettingsFrame();

        assertNotNull(frame.settings());
        assertFalse(frame.iterator().hasNext());
        assertNull(frame.get(Http3SettingIdentifier.HTTP3_SETTINGS_QPACK_MAX_TABLE_CAPACITY.id()));
    }

    @Test
    void testPutAndGetDelegatesToSettings() {
        DefaultHttp3SettingsFrame frame = new DefaultHttp3SettingsFrame();

        frame.put(Http3SettingIdentifier.HTTP3_SETTINGS_QPACK_MAX_TABLE_CAPACITY.id(), 256L);
        frame.put(Http3SettingIdentifier.HTTP3_SETTINGS_QPACK_BLOCKED_STREAMS.id(), 8L);

        assertEquals(256L, frame.settings().qpackMaxTableCapacity());
        assertEquals(8L, frame.settings().qpackBlockedStreams());
        assertEquals(8L, frame.get(Http3SettingIdentifier.HTTP3_SETTINGS_QPACK_BLOCKED_STREAMS.id()));
    }

    @Test
    void testPutRejectsReservedHttp2Settings() {
        DefaultHttp3SettingsFrame frame = new DefaultHttp3SettingsFrame();
        assertThrows(IllegalArgumentException.class, () -> frame.put(0x4, 10L)); // HTTP/2 reserved key
    }

    @Test
    void testSettingsReferenceIsRetained() {
        Http3Settings settings = new Http3Settings().qpackMaxTableCapacity(512);
        DefaultHttp3SettingsFrame frame = new DefaultHttp3SettingsFrame(settings);

        assertSame(settings, frame.settings());
        assertEquals(512L, frame.settings().qpackMaxTableCapacity());

        // Modify settings externally and verify frame sees update
        settings.qpackBlockedStreams(3);
        assertEquals(3L, frame.get(Http3SettingIdentifier.HTTP3_SETTINGS_QPACK_BLOCKED_STREAMS.id()));
    }

    @Test
    void testEqualsAndHashCodeCompareSettings() {
        Http3Settings s1 = new Http3Settings().qpackMaxTableCapacity(128);
        Http3Settings s2 = new Http3Settings().qpackMaxTableCapacity(128);

        DefaultHttp3SettingsFrame f1 = new DefaultHttp3SettingsFrame(s1);
        DefaultHttp3SettingsFrame f2 = new DefaultHttp3SettingsFrame(s2);

        assertEquals(f1, f2);
        assertEquals(f1.hashCode(), f2.hashCode());
    }

    @Test
    void testIteratorReflectsSettingsEntries() {
        Http3Settings settings = new Http3Settings()
                .qpackMaxTableCapacity(64)
                .qpackBlockedStreams(2);
        DefaultHttp3SettingsFrame frame = new DefaultHttp3SettingsFrame(settings);

        Iterator<Map.Entry<Long, Long>> it = frame.iterator();
        int count = 0;
        while (it.hasNext()) {
            Map.Entry<Long, Long> entry = it.next();
            assertNotNull(entry.getKey());
            assertNotNull(entry.getValue());
            count++;
        }
        assertTrue(count >= 2);
    }

    @Test
    void testToStringIncludesSettings() {
        Http3Settings settings = new Http3Settings()
                .qpackMaxTableCapacity(1)
                .enableConnectProtocol(true);

        DefaultHttp3SettingsFrame frame = new DefaultHttp3SettingsFrame(settings);
        String output = frame.toString();

        assertTrue(output.contains("Http3Settings"));
        assertTrue(output.contains("0x1"));
        assertTrue(output.contains("settings="));
    }

    @Test
    void testCopyOfCreatesDeepCopy() {
        DefaultHttp3SettingsFrame original = new DefaultHttp3SettingsFrame(
                new Http3Settings().qpackMaxTableCapacity(10).enableConnectProtocol(true));

        DefaultHttp3SettingsFrame copy = DefaultHttp3SettingsFrame.copyOf(original);

        assertNotSame(original, copy);
        assertEquals(original, copy);

        // Modify original and ensure copy remains unchanged
        original.put(Http3SettingIdentifier.HTTP3_SETTINGS_QPACK_BLOCKED_STREAMS.id(), 5L);
        assertNotEquals(original, copy);
    }

    @Test
    void testDeprecatedMethodsStillWork() {
        DefaultHttp3SettingsFrame frame = new DefaultHttp3SettingsFrame();
        frame.put(Http3SettingIdentifier.HTTP3_SETTINGS_QPACK_BLOCKED_STREAMS.id(), 5L);

        // Uses old get() API
        assertEquals(5L, frame.get(Http3SettingIdentifier.HTTP3_SETTINGS_QPACK_BLOCKED_STREAMS.id()));

        // New typed accessor matches
        assertEquals(5L, frame.settings().qpackBlockedStreams());
    }

    @Test
    void testDuplicateKeyInSettingsFrameTriggersError() {
        DefaultHttp3SettingsFrame frame = new DefaultHttp3SettingsFrame();

        frame.put(0x1, 10L);
        assertThrows(IllegalStateException.class, () -> {
            // Simulate protocol behavior
            if (frame.put(0x1, 20L) != null) {
                throw new IllegalStateException("Duplicate SETTINGS key detected");
            }
        });
    }

}
